package com.jinlejia.erp.ui.customer;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.jinlejia.erp.R;
import com.jinlejia.erp.adapter.HouseFollowAdapter;
import com.jinlejia.erp.adapter.ViewPagerAdapter;
import com.jinlejia.erp.databinding.ActivityCustomerFllowBinding;
import com.jinlejia.erp.utils.Player;

import java.util.ArrayList;
import java.util.List;

/**
 * 客源跟进
 */
public class CustomerFllowActivity extends AppCompatActivity {


    // 填充到ViewPager中的Fragment
    private List<Fragment> mFragments;
    //Tab选项卡的文字
    private String mTitleArray[] = {"私有有效", "私有无效", "公开有效", "公开无效"};
    private ViewPagerAdapter viewPagerAdapter;
    HouseFollowAdapter adapter;

    private int pageIndex = 1;
    private boolean mLoadingLock = false;
    Player mPlayer;
    private ActivityCustomerFllowBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_customer_fllow);
        binding.btBack.setOnClickListener(this::onClick);
        binding.bottomLayout.setOnClickListener(this::onClick);
        mFragments = new ArrayList<>();
        String code = getIntent().getStringExtra("code");
        mFragments.add(0, CustomerFollowListFragment.newInstance(code,"5"));
        mFragments.add(1, CustomerFollowListFragment.newInstance(code,"6"));
        mFragments.add(2, CustomerFollowListFragment.newInstance(code,"7"));
        mFragments.add(3, CustomerFollowListFragment.newInstance(code,"8"));

        viewPagerAdapter = new ViewPagerAdapter(this.getSupportFragmentManager(), mTitleArray, mFragments);
        binding.viewPager.setAdapter(viewPagerAdapter);
        binding.viewPager.setOffscreenPageLimit(4);

        // 设置Tablayout的Tab显示ViewPager的适配器中的getPageTitle函数获取到的标题
        binding.idTableLayout.setTabsFromPagerAdapter(viewPagerAdapter);
        binding.idTableLayout.post(() -> {
            // 将TabLayout和ViewPager进行关联，让两者联动起来
            binding.idTableLayout.setupWithViewPager(binding.viewPager);
        });

    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_back:
                finish();
                break;
            case R.id.bottom_layout:
                //写跟进
                Intent intent = new Intent(CustomerFllowActivity.this, WriteCustomerFollowActivity.class);
                intent.putExtra("code", getIntent().getStringExtra("code"));
                intent.putExtra("type", getIntent().getStringExtra("type"));
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
//        getData();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if( null != mPlayer )
        mPlayer.stop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if( null != mPlayer )
        mPlayer.stop();
    }
}
